#include "MagicCubeScene.h"
#include "Cube.h"
#include <QDebug>
namespace GCL {  namespace MagicCube {

MagicCubeScene::MagicCubeScene()
{
    this->initCubes();
}

MagicCubeScene::~MagicCubeScene()
{

}

void MagicCubeScene::initCubes()
{
    float x,y,z;
    for(z = -0.2; z < 0.3; z+=0.2)
        for(x = -0.2; x < 0.3; x+=0.2)
            for(y = -0.2; y < 0.3; y+=0.2)
                    cubes_.push_back(std::shared_ptr<Cube>(new Cube(Vec3(x,y,z),0.1)));
}


//------选择魔方对应面
void MagicCubeScene::selectCubeFace(int select_face)
{
    int k = 0;
    int num = cubes_.size();
    for(int i=0; i<num; i++)
    {
        if(cubes_[i]->getCenter(select_face/2) >= -max_range && cubes_[i]->getCenter(select_face/2) <= -min_range && select_face % 2 == 1)
        {
            cubeNumber_[k] = i;
            k++;
        }
        if(cubes_[i]->getCenter(select_face/2) <= max_range && cubes_[i]->getCenter(select_face/2) >= min_range && select_face % 2 == 0)
        {
            cubeNumber_[k] = i;
            k++;
        }
    }
}


//对应面魔方选中后画线
void MagicCubeScene::drawDiagnolline()
{
    for(int i=0; i<9; ++i)
        cubes_[cubeNumber_[i]]->renderDiagonal();
}

void MagicCubeScene::renderObjects()
{
    for(int i=0; i < cubes_.size(); i++)
    {
        cubes_[i]->render();
    }
    if(draw_diagonal_signal_ == true)
        drawDiagnolline();
}

void MagicCubeScene::timerRun()
{
    Scene::timerRun();
}



//设置按键响应事件
void MagicCubeScene::keyUp(const unsigned int &key, unsigned int modifier)
{
    int select_face = 0;
    switch (key)
    {
        case Qt::Key_Q:
        {
            if(rotato_account_ == 0)
            {
                select_face = 5;
                axis_ = {0,0,1};
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;
        }
        case Qt::Key_W:
        {
            if(rotato_account_ == 0)
            {
                select_face = 4;
                axis_ = {0,0,-1};
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;
        }
        case Qt::Key_E:
        {
            if(rotato_account_ == 0)
            {
                select_face = 3;
                axis_ = {0,1,0};
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;

        }
        case Qt::Key_A:
        {
            if(rotato_account_ == 0)
            {
                axis_ = {0,-1,0};
                select_face = 2;
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;

        }
        case Qt::Key_S:
        {
            if(rotato_account_ ==0)
            {
                axis_ = {1,0,0};
                select_face = 1;
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;
        }
        case Qt::Key_D:
        {
            if(rotato_account_ == 0)
            {
                axis_ = {-1,0,0};
                select_face = 0;
                draw_diagonal_signal_ = true;
                selectCubeFace(select_face);
            }
            break;

        }
        case Qt::Key_Z:
        {
            key_list_.push_back(Key_Z);
            cleanKey();
            break;
        }
        case Qt::Key_X:
        {
            key_list_.push_back(Key_X);
            cleanKey();
            break;
        }
        case Qt::Key_C:
        {
            draw_diagonal_signal_ = false;
            break;
        }
        default:
            break;
    }
    Scene::keyUp(key,modifier);
}

//------魔方旋转

void MagicCubeScene::cubeRotato()
{
    if(cube_rotato_control_ == true)
    {
        for(int i=0; i<9; ++i)
            cubes_[cubeNumber_[i]]->rotate(axis_temp_,PI/60);
        rotato_account_++;
        if(rotato_account_ == 30)
        {
           cube_rotato_control_ = false;
           rotato_account_ = 0;
           clean_key_signal_ = true;
           cleanKey();
        }
     }

}


void MagicCubeScene::cleanKey()
{
    if(key_list_.size())
    {
        if(rotato_account_ == 0 && clean_key_signal_ == true)
        {
            clean_key_signal_ = false;
            int tempKey = key_list_.first();
            key_list_.removeFirst();
            if(tempKey == Key_Z)
                axis_temp_ = axis_;
            if(tempKey == Key_X)
                axis_temp_ = -axis_;
        }
        cube_rotato_control_ = true;
        cubeRotato();
    }
}

}}
